import { defineStore } from 'pinia'
import { Part } from '@/types/system/device-maintenance/part'
import { useDeviceMaintenanceProjectStore } from './project'
import { getNodeIdsAsync } from '@/api/axios/device-server-client'
import * as api from '@/api/axios/device-maintenance-client'
import { ref, computed, watch } from 'vue'
import { ShowDeleteMessageBoxAsync } from '@/utils/message-box'

export const useDeviceMaintenancePartStore = defineStore('device-maintenance-part', () => {
  const isLoading = ref(false)
  const isNeedLoadParts = ref(true)
  const currentProjectName = ref('')
  const isSelectProjectName = ref(false)
  let oldCurrentProjectName = ''
  const parts = ref<Part[]>([])
  const nodeIds = ref<string[] | null>(null)

  const projectNames = computed(() => {
    const store = useDeviceMaintenanceProjectStore()
    return store.projectNames
  })

  watch(currentProjectName, async (newValue) => {
    if (newValue != oldCurrentProjectName) {
      oldCurrentProjectName = newValue
      if (oldCurrentProjectName == '' || oldCurrentProjectName == null) {
        isSelectProjectName.value = false
      } else {
        isSelectProjectName.value = true
      }
      await updatePartsAsync()
    }
  })

  function openLoading() {
    isLoading.value = true
  }

  function closeLoading() {
    isLoading.value = false
  }

  // 更新部件
  async function updatePartsAsync() {
    if (nodeIds.value == null) {
      openLoading()
      const result = await getNodeIdsAsync()
      if (result.isSuccess) {
        nodeIds.value = result.result
      } else {
        closeLoading()
        return false
      }
    }
    const store = useDeviceMaintenanceProjectStore()
    // 判断是否需要加载项目信息
    if (store.isNeedLoadProjects) {
      openLoading()
      const result = await store.updateProjectsAsync()
      if (!result) {
        closeLoading()
        return false
      }
    }
    const currentProject = store.projects.find(
      (item) => item.projectName == currentProjectName.value
    )
    if (currentProject != null) {
      openLoading()
      const result = await api.getPartsAsync(currentProject.id)
      if (result.isSuccess) {
        closeLoading()
        parts.value = result.result ?? []
        isNeedLoadParts.value = false
        return true
      }
    }
    closeLoading()
    return false
  }

  // 添加部件
  async function insertPartAsync(newPart: Part) {
    openLoading()
    const result = await api.insertPartAsync(newPart)
    if (result.isSuccess) {
      await updatePartsAsync()
    }
    closeLoading()
    return result.isSuccess
  }

  // 删除部件
  async function deletePartAsync(index: number) {
    if (!(await ShowDeleteMessageBoxAsync())) {
      return
    }
    openLoading()
    const result = await api.deletePartAsync(parts.value[index].id)
    if (result.isSuccess) {
      updatePartsAsync()
    }
    closeLoading()
    return result.isSuccess
  }

  function tryGetCurrentProjectId() {
    const store = useDeviceMaintenanceProjectStore()
    const currentProject = store.projects.find(
      (item) => item.projectName == currentProjectName.value
    )
    return currentProject?.id ?? null
  }

  return {
    isLoading,
    isNeedLoadParts,
    currentProjectName,
    isSelectProjectName,
    parts,
    nodeIds,
    projectNames,
    updatePartsAsync,
    insertPartAsync,
    deletePartAsync,
    tryGetCurrentProjectId
  }
})
